<template>
    <div>
      <header class="header">
        <el-button @click="$router.push('/student/dashboard')" icon="left" type="text" class="back-button">
          <img src="@/assets/svgs/left.svg" alt="Back" />
        </el-button>
        <span class="title" style="margin-left: 10px;">AIGC作业</span>
      </header>
      <div><br></div>
      <el-divider content-position="left" style="color: #13227a;">我的作业</el-divider>
      <div><br></div>
      <div class="homework-card-list">
        <div v-for="homework in homeworks" :key="homework.id" class="homework-card" @click="$router.push('/student/homework/submit/submit')">
          <div>
            <div class="homework-title">{{ homework.title }}</div>
            <div class="homework-teacher">教师：{{ homework.teacher }}</div>
            <div class="homework-dates">
              <div class="publish-time">发布时间：{{ formatDate(homework.publishTime) }}</div>
              <div class="due-time">截止时间：{{ formatDate(homework.dueTime) }}</div>
            </div>
          </div>
          <div class="homework-submission">
            <div class="submission-status">
              <div class="status-text">{{ getSubmissionStatus(homework) }}</div>
              <div class="status-label">作业状态</div>
            </div>
            <div class="submission-score">
              <div class="score-number">{{ homework.score }}</div>
              <div class="score-label">分数</div>
            </div>
          </div>
        </div>
        <div class="homework-card" @click="$router.push('/student/homework/see_correcthomework')">
          <div>
            <div class="homework-title">MySQL</div>
            <div class="homework-teacher">教师：张老师</div>
            <div class="homework-dates">
              <div class="publish-time">发布时间：2024年11月9日12：00</div>
              <div class="due-time">截止时间：2024年12月9日12：00</div>
            </div>
          </div>
          <div class="homework-submission">
            <div class="submission-status">
              <div class="status-text">已批改</div>
              <div class="status-label">作业状态</div>
            </div>
            <div class="submission-score">
              <div class="score-number">100</div>
              <div class="score-label">分数</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { useRoute } from 'vue-router';
  import { ref } from 'vue';

  const route = useRoute();
  const homeworkId = route.params.id; // 获取作业 ID
  </script>
  
  <script>
  export default {
    data() {
      return {
        homeworks: [
          // 示例数据
          {
            id: 1,
            title: '数学作业',
            teacher: '张老师',
            publishTime: new Date(),
            dueTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1天后截止
            score: 100
          },
          {
            id: 2,
            title: 'Maven作业',
            teacher: '张老师',
            publishTime: new Date(),
            dueTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1天后截止
            score: 100
          },
          {
            id: 3,
            title: '数学作业',
            teacher: '张老师',
            publishTime: new Date(),
            dueTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1天后截止
            score: 100
          },
          {
            id: 4,
            title: 'Maven作业',
            teacher: '张老师',
            publishTime: new Date(),
            dueTime: new Date(new Date().getTime() + 24 * 60 * 60 * 1000), // 1天后截止
            score: 100
          },
          // 可以添加更多作业
        ]
      };
    },
    methods: {
      formatDate(date) {
        const options = { 
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit',
          hour12: false
        };
        // 将默认格式转换为指定格式
        return new Intl.DateTimeFormat('zh-CN', options)
          .format(date)
          .replace(/\//g, '-')  // 替换斜杠为横杠
          .replace(/-/g, '年', 1)  // 第一个横杠替换为年
          .replace(/-/g, '月', 1)  // 第二个横杠替换为月
          .replace(/\s/, '日 ');   // 空格前添加"日"
      },
      getSubmissionStatus(homework) {
        const now = new Date();
        const dueTime = new Date(homework.dueTime);
        
        if (now > dueTime) {
          return homework.score || '未提交';  // 如果超过截止时间，显示分数
        } else {
          return homework.isSubmitted ? '已提交' : '待提交';  // 在截止时间内，显示提交状态
        }
      }
    }
  };
  </script>
  
  <style scoped>
  .header {
    color: white;
    height: 30px;
    padding: 10px;
    display: flex;
    align-items: center;
    background-color: #13227a;
    justify-content: flex-start; /* 修改为左对齐 */
  }
  
  .back-button {
    color: white;
    border: none;
    width: 25px;
    height: 25px;
  }
  
  .el-divider--horizontal {
    margin: 0; /* 移除默认的 margin */
    color: #13227a;
  }

  .title {
    margin-left: 10px; /* 添加左边距 */
  }
  
  .header-right {
    display: flex; /* 使右侧内容为flex布局 */
  }
  
  .assign-button {
    background-color: #13227a;
    border: 1px solid white;
    color: white;
    width: 120px;
    height: 40px;
  }
  
  .homework-card-list {
    display: grid;
    grid-template-columns: repeat(3, 1fr); /* 强制三列布局 */
    gap: 20px;
    padding: 0 40px;
  }
  
  .homework-card {
    background-color: white;
    border: 1px solid #ccc;
    border-radius: 5px;
    padding: 10px;
    height: 220px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  
  .homework-title {
    font-weight: bold;
    color: black;
    font-size: 20px;
  }
  
  .homework-teacher {
    color: gray;
    font-size: 14px;
    margin-top: 20px;
  }
  
  .homework-dates {
    color: gray;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    margin-top: 10px;
  }
  
  .publish-time {
    text-align: left;
  }
  
  .due-time {
    text-align: right;
  }
  
  .homework-submission {
    color: darkgray;
    display: flex;
    justify-content: space-between; /* 改为两端对齐 */
    align-items: center;
    margin-top: 40px;
    padding: 0 20px; /* 添加左右内边距 */
  }
  
  .submission-status {
    text-align: left; /* 左对齐 */
  }
  
  .status-text {
    font-size: 20px;
    color: #13227a; /* 使用主题蓝色 */
  }
  
  .status-label {
    font-size: 14px;
    color: darkgray;
  }
  
  .submission-score {
    text-align: right; /* 右对齐 */
  }
  </style>